requireJS(['constant', 'site', 'handlebars'], function (Constant, site, Handlebars) {
    Handlebars.registerHelper('addrepay', function (v1, v2, opts) {

        return v1 + v2;

    });

    var url = {
        selectLoanMethodList: "manager/selectLoanMethodList.do",
        selectRepanymentWayList:"manager/selectRepanymentWayList.do"
    }

    //渲染搜索框的贷款方式
    site.send({
        url: url.selectLoanMethodList,
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == '1') {
                var source = $("#loanMethod_list_tpl").html();
                var template = Handlebars.compile(source);
                var list = data
                var html = template(list)
                $('#loan_way').append(html);
            }
        }
    })

    //渲染搜索框的还款方式
    site.send({
        url: url.selectRepanymentWayList,
        callback: function (data) {
            data = JSON.parse(data);
            if (data.type == '1') {
                var source = $("#repaymentWay_list_tpl").html();
                var template = Handlebars.compile(source);
                var list = data
                var html = template(list)
                $('.repayment_way').append(html);
            }
        }
    })

    //初始化数据
    var repaymentData = {
        pageNum: 1,
        // gps:1,//
        repayment_way: 2,//选择还款方式:
        // aa:1,//选择贷款方式
        month_repayment_money: 200000,//设置借款金额
        num: 24,//设置借款金额
        moth_repayment_rate: 0.3,//输入利率
        // start_time:2018-04-01
    }
    var initData = function () {
        // $("#tableShow").hide();//结果显隐藏
        site.send({
            url: "manager/selectApplyTermList.do",
            //async:false
            data: {},
            callback: function (data) {
                var list = JSON.parse(data);
                var template = Handlebars.compile($("#repayment_period_tpl").html())
                var html = template(list);
                $("#repayment_period").html(html)
            }
        })
    }
    initData()

    // 计算
    $(".tabHint-alone").click(function () {
        repaymentData.repayment_way = $(".repayment_way").val();
        repaymentData.loan_way = $("#loan_way").val();
        // repaymentData.aa=$(".aa").val()
        repaymentData.month_repayment_money = $(".month_repayment_money").val()
        repaymentData.num = $(".num").val()
        repaymentData.moth_repayment_rate = $(".moth_repayment_rate").val()
        // repaymentData.start_time=$(".start_time").val()

        if(!repaymentData.repayment_way){
            Constant.fn.Message("I","请选择还款方式");
            return;
        }

        if(!repaymentData.loan_way){
            Constant.fn.Message("I","请选择贷款方式");
            return;
        }

        if(!repaymentData.month_repayment_money){
            Constant.fn.Message("I","请设置借款金额");
            return;
        }

        if(!repaymentData.num){
            Constant.fn.Message("I","请选择期限");
            return;
        }

        if(!repaymentData.moth_repayment_rate){
            Constant.fn.Message("I","请输入利率");
            return;
        }

        site.send({
            url: "manager/repaymentCalculator.do",
            //async:false
            data: repaymentData,
            callback: function (data) {
                var list = JSON.parse(data);
                if (list.type == 1) {
                    if(list.content[0]!=null){
                        var source = $("#repayment_jieguo_tpl").html();
                        var template = Handlebars.compile(source);
                        var html = template(list.content);
                        $('#repayment_jieguo').html(html);
                    }else{
                        Constant.fn.Message("F","查询为空");
                    }

                }
            }
        })
    })
    $(".datepicker").datepicker({
        format: 'yyyy-mm-dd',
        autoclose: true
    })
})
